##*****************************************************************************
## TABLE 1
##*****************************************************************************
## 
## R Version: R version 3.2.4 (2016-03-1)
##

##LOADING PACKAGES
library(texreg) ##Version: 1.36.4

##SET WORKING DIRECTORY
setwd('/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
cert.data <- read.csv('replication_materials/data/cert_petition_data_matched_to_dime.csv') 
 
##CASES DECIDED UNANIMOUSLY
cert.data$unam <- ifelse(cert.data$minVotes == 0 ,1,0)

##REGRESSION MODELS

##
##UNANIMIOUS CASES
##

##MODEL 1
m1 <- (glm(I(ddirect)~cfscore_pet ,data=cert.data,subset=unam==1 & decisionDirection!=3,family=binomial()))
tt1 <- table(predict(m1)>0,m1$y);sum(diag(tt1))/sum(tt1)

##MODEL 2
m2 <- (glm(I(ddirect)~cfscore_pet + cfscore_resp,data=cert.data,subset=unam==1 & decisionDirection!=3,family=binomial()))
tt2 <- table(predict(m2)>0,m2$y);sum(diag(tt2))/sum(tt2)

##MODEL 3
m3 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp),data=cert.data,subset=unam==1 & decisionDirection!=3,family=binomial()))
tt3 <- table(predict(m3)>0,m3$y);sum(diag(tt3))/sum(tt3)

##MODEL 4
m4 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp) + factor(issueAreaName),data=cert.data,subset=unam==1 & decisionDirection!=3,family=binomial()))
tt4 <- table(predict(m4)>0,m4$y);sum(diag(tt4))/sum(tt4) 

screenreg(list(m1,m2,m3,m4))
texreg(list(m1,m2,m3,m4),omit='issueArea',
       stars = c(0),
       custom.coef.names=c('(Intercept)',
           'DIME score of Cert.Dataing Atty.',
           'DIME score of Respondent Atty.',
           '(DIME score of Cert.Dataing Atty. $-$ DIME score of Respondent Atty.)',
           names(m4$coefficients)[-c(1:2)]),
       file='/workspace/fed_courts/scaling_paper/replication_materials/tables/table_A1_voting_with_judges_unam.tex')


##
##NON-UNANIMIOUS CASES
##

##MODEL 1 
nu1 <- (glm(I(ddirect)~cfscore_pet ,data=cert.data,subset=unam==0 & decisionDirection!=3,family=binomial()))
tt1 <- table(predict(nu1)>0,nu1$y);sum(diag(tt1))/sum(tt1)

##MODEL 2
nu2 <- (glm(I(ddirect)~cfscore_pet + cfscore_resp,data=cert.data,subset=unam==0 & decisionDirection!=3,family=binomial()))
tt2 <- table(predict(nu2)>0,nu2$y);sum(diag(tt2))/sum(tt2)

##MODEL 3
nu3 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp),data=cert.data,subset=unam==0 & decisionDirection!=3,family=binomial()))
tt3 <- table(predict(nu3)>0,nu3$y);sum(diag(tt3))/sum(tt3)

##MODEL 4
m4 <- (glm(I(ddirect)~I(cfscore_pet - cfscore_resp) + factor(issueAreaName),data=cert.data,subset=unam==0 & decisionDirection!=3,family=binomial()))
tt4 <- table(predict(m4)>0,m4$y);sum(diag(tt4))/sum(tt4) 

screenreg(list(nu1,nu2,nu3,m4))
texreg(list(nu1,nu2,nu3,m4),omit='issueArea',
       stars = c(0),
       custom.coef.names=c('(Intercept)',
           'DIME score of Cert.Dataing Atty.',
           'DIME score of Respondent Atty.',
           '(DIME score of Cert.Dataing Atty. $-$ DIME score of Respondent Atty.)',
           names(m4$coefficients)[-c(1:2)]),
       file='/workspace/fed_courts/scaling_paper/replication_materials/tables/table_A2_voting_with_judges_unam.tex')

